package com.cgtong.android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.cgtong.android.App;
import com.cgtong.android.R;
import com.cgtong.android.base.BaseActivity;
import com.cgtong.android.db.UserAccount;
import com.cgtong.android.http.ApiManager;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.util.CommonUtils;
import com.umeng.analytics.MobclickAgent;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.ApiResult;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 修改密码
 */

public class ModifyPwdActivity extends BaseActivity {

    @BindView(R.id.title_center_txt)
    TextView mTitleCenterTxt;
    @BindView(R.id.et_pwd0)
    TextView et_pwd0;
    @BindView(R.id.et_pwd1)
    TextView et_pwd1;
    @BindView(R.id.et_pwd2)
    TextView et_pwd2;

    private UserAccount account;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTitleCenterTxt.setText(getString(R.string.title_pwd));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_modify_pwd;
    }

    @Override
    public void initData() {
    }

    private void commit() {
        KeyboardUtils.hideSoftInput(this);
        String pwd0 = et_pwd0.getText().toString();
        String pwd1 = et_pwd1.getText().toString();
        String pwd2 = et_pwd2.getText().toString();

        if (TextUtils.isEmpty(pwd0)) {
            ToastUtils.showLong(R.string.txt_hint_orig);
            return;
        }
        if (TextUtils.isEmpty(pwd1)) {
            ToastUtils.showLong(R.string.txt_hint_new);
            return;
        }
        if (!CommonUtils.checkPasswordRule(pwd1)) {
            ToastUtils.showLong(R.string.txt_irregular_password);
            return;
        }
        if (TextUtils.isEmpty(pwd2)) {
            ToastUtils.showLong(R.string.txt_hint_agin);
            return;
        }

        if (!pwd2.equals(pwd1)) {
            ToastUtils.showLong(R.string.txt_hint_agin2);
            return;
        }

        EasyHttp.post(ApiManager.URL_UPDATEPWD)
                .params("password", pwd0)
                .params("newPassword", pwd1)
                .params("confirmPassword", pwd2)
                .execute(new SimpleCallBack<List<ApiResult>>() {
                    @Override
                    public void onError(ApiException e) {
                        ToastUtils.showShort(e.getMessage());
                    }

                    @Override
                    public void onSuccess(List<ApiResult> result) {
                        ToastUtils.showShort("修改成功");
//                        finish();

                        //退出登录
                        Intent intent = new Intent(ModifyPwdActivity.this, LoginActivity.class);
                        intent.putExtra(LoginActivity.EXTRA_LOGINED_SHOW_PAGE, ConstantManager.MAIN_PAGE_INDEX);
                        startActivity(intent);
                        if (account == null) {
                            account = App.getInstance().getCurrentAccount();
                        }
                        account.setPassword("");
                        account.setToken("");
                        account.setLoginStatus(ConstantManager.LOGIN_OUT);
                        App.getInstance().updateAccount(account);
                        App.getInstance().finishAllActivity();//关闭所有Activity
                    }
                });
    }

    @OnClick({R.id.title_left_txt, R.id.bt_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_txt:
                finish();
                break;
            case R.id.bt_ok:
                commit();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("修改密码");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("修改密码");
        MobclickAgent.onPause(this);
    }
}
